var orgId = '1';
var dvcGroupId = 0;
$.ajax({
	url: 'dvcList',
	type: 'POST',
	data: {
		companyId: $.Cookie("companyId"),
		session: $.Cookie("session"),
		userId: $.Cookie("userId"),
		orgId: 1,
		index: 0,
		page: 10
	},
	dataType: 'json',
	success: function(data) {
		if(data.errorCode == 0) {
			$.ajax({
				url: 'orgListTree',
				type: 'POST',
				data: {
					companyId: $.Cookie("companyId"),
					session: $.Cookie("session"),
					userId: $.Cookie("userId"),
				},
				dataType: 'json',
				success: function(data) {
					if(data.errorCode == 0) {
						var setting = {
							data: {

								simpleData: {
									enable: true
								}
							},
							callback: {
								beforeClick: beforeClick

							}
						};
						var zNodes = data.result
						var log, className = "dark";

						function beforeClick(treeId, treeNode, clickFlag) {
							orgId = treeNode.orgId;
							$.ajax({
								url: 'dvcList',
								type: 'POST',
								data: {
									session: $.Cookie("session"),
									userId: $.Cookie("userId"),
									companyId: $.Cookie("companyId"),
									orgId: treeNode.orgId,
									dvcGroupId: dvcGroupId,
									index: '0',
									page: '10'
								},
								dataType: 'json',
								success: function(data) {
									if(data.errorCode == 0) {
										tickId = 0;
										tickaId = '';
										$(".pageToolDevice").empty()
										$.each(data.result.dvcList, function(i, value) {
											var nDate = new Date(value.createdTime); //直接将毫秒数当参数传进去。
											var month = nDate.getMonth() + 1;
											var year = nDate.getFullYear() + '-' + month + '-' + nDate.getDate() + " " + nDate.getHours() + ':' + nDate.getMinutes() + ':' + nDate.getSeconds();
											var error_tpl;
											if(value.status == 0) {
												if(i % 2 == 0) {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
												} else {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
												}
											} else {
												if(i % 2 == 0) {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

												} else {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

												}

											}

											$(".pageToolDevice").append(error_tpl)

										});
									} else if(data.errorCode == -6) {
										alert("登录超时！")
										top.location.href = 'index.jsp';

									}
								},
								error: function(err) {
									alert("连接服务器失败！请重新提交！");
									top.location.href = 'index.jsp';

								}
							});
						}
						$(document).ready(function() {
							$.fn.zTree.init($("#treeDemoDevicea"), setting, zNodes);
						});
					} else if(data.errorCode == -6) {
						alert("登录超时！")
						top.location.href = 'index.jsp';
					}
				},
				error: function(err) {
					alert("连接服务器失败！请重新提交！")
					top.location.href = 'index.jsp';

				}
			});
			//		请求树形设备
			$.ajax({
				url: 'dvcGroupTree',
				type: 'POST',
				data: {
					companyId: $.Cookie("companyId"),
					session: $.Cookie("session"),
					userId: $.Cookie("userId"),
				},
				dataType: 'json',
				success: function(data) {
					if(data.errorCode == 0) {

						var setting = {
							data: {
								simpleData: {
									enable: true
								}
							},
							callback: {
								beforeClick: beforeClick2

							}
						};

						var zNodes = data.result

						var log, className = "dark";

						function beforeClick2(treeId, treeNode, clickFlag) {
							dvcGroupCode = treeNode.dvcGroupCode
							//					console.log(treeNode.dvcGroupCode)
							$.ajax({
								url: 'dvcList',
								type: 'POST',
								data: {
									session: $.Cookie("session"),
									userId: $.Cookie("userId"),
									companyId: $.Cookie("companyId"),
									orgId: orgId,
									dvcGroupId: treeNode.dvcGroupCode,
									index: '0',
									page: '10'
								},
								dataType: 'json',
								success: function(data) {
									if(data.errorCode == 0) {
										tickId = 0;
										tickaId = '';
										$(".pageToolDevice").empty()
										$.each(data.result.dvcList, function(i, value) {
											var error_tpl;
											var nDate = new Date(value.createdTime); //直接将毫秒数当参数传进去。
											var month = nDate.getMonth() + 1;
											var year = nDate.getFullYear() + '-' + month + '-' + nDate.getDate() + " " + nDate.getHours() + ':' + nDate.getMinutes() + ':' + nDate.getSeconds();

											if(value.status == 0) {
												if(i % 2 == 0) {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
												} else {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
												}
											} else {
												if(i % 2 == 0) {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

												} else {
													error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

												}

											}

											$(".pageToolDevice").append(error_tpl)

										});
									} else if(data.errorCode == -6) {
										alert("登录超时！")
										top.location.href = 'index.jsp';
									}
								},
								error: function(err) {
									alert("连接服务器失败！请重新提交！")
									top.location.href = 'index.jsp';

								}
							});
						}

						$(document).ready(function() {
							$.fn.zTree.init($("#treeDemoDevice"), setting, zNodes);
						});
					} else if(data.errorCode == -6) {
						alert("登录超时！")
						top.location.href = 'index.jsp';
					}
				},
				error: function(err) {
					alert("连接服务器失败！请重新提交！")
					top.location.href = 'index.jsp';
				}
			});
			$.each(data.result.dvcList, function(i, value) {
				var nDate = new Date(value.createdTime); //直接将毫秒数当参数传进去。
				var month = nDate.getMonth() + 1;
				var year = nDate.getFullYear() + '-' + month + '-' + nDate.getDate() + " " + nDate.getHours() + ':' + nDate.getMinutes() + ':' + nDate.getSeconds();
				var error_tpl;
				if(value.status == 0) {
					if(i % 2 == 0) {
						error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
					} else {
						error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
					}
				} else {
					if(i % 2 == 0) {
						error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

					} else {
						error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

					}

				}
				$(".pageToolDevice").append(error_tpl)

			});
			$('.pageToolDevicea').Paging({
				pagesize: 10,
				count: data.result.count,
				toolbar: true,
				callback: function(page, size, count) {
					$(".pageToolDevice").html("");
					$.ajax({
						url: 'dvcList',
						type: 'POST',
						data: {
							companyId: $.Cookie("companyId"),
							session: $.Cookie("session"),
							userId: $.Cookie("userId"),
							orgId: 1,
							index: page - 1,
							page: 10
						},
						dataType: 'json',
						success: function(data) {
							if(data.errorCode == 0) {
								$.each(data.result.dvcList, function(i, value) {
									if(data.errorCode == 0) {
										var nDate = new Date(value.createdTime); //直接将毫秒数当参数传进去。
										var month = nDate.getMonth() + 1;
										var year = nDate.getFullYear() + '-' + month + '-' + nDate.getDate() + " " + nDate.getHours() + ':' + nDate.getMinutes() + ':' + nDate.getSeconds();
										var error_tpl;
										if(value.status == 0) {
											if(i % 2 == 0) {
												error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
											} else {
												error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + ' </div><div class="manage_content_header_div">启用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'
											}
										} else {
											if(i % 2 == 0) {
												error_tpl = '<div ondblclick="ondblInput()" class="device_content_contenta"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

											} else {
												error_tpl = '<div ondblclick="ondblInput()" class="device_content_content"><div onclick="checkInputA(this)" class="content_input_b" id="' + value.id + '"><div id="tick_' + value.id + '" class="tick">√</div></div><div class="manage_content_header_div">' + value.dvcId + '</div><div class="manage_content_header_div">' + value.dvcType + '</div><div class="manage_content_header_div">禁用</div><div class="device_content_header_one">' + value.orgName + '</div><div class="manage_content_header_div">' + value.remark + '</div><div class="manage_content_header_div">' + year + '</div></div>'

											}

										}
										$(".pageToolDevice").append(error_tpl)
									} else if(data.errorCode == -6) {
										alert("登录超时！")
										top.location.href = 'index.jsp';
									}
								});

							} else if(data.errorCode == -6) {
								alert("登录超时！")
								top.location.href = 'index.jsp';

							}

						},
						error: function(err) {
							alert("连接服务器失败！请重新提交！");
							top.location.href = 'index.jsp';
						}
					});

				}
			});
		} else if(data.errorCode == -6) {
			alert("登录超时！")
			top.location.href = 'index.jsp';
		}

	},
	error: function(err) {
		alert("连接服务器失败！请重新提交！");
		top.location.href = 'index.jsp';
	}
});

//点击显示隐藏
$("#getDevice_frist").click(function() {
	if($(".divice_header_frist").css("display") == 'block') {
		$(".add").addClass("device_add");
		$(".file").addClass("device_file");
		$(".file").removeClass("device-file");
		$(".add").removeClass("device-add");
		$(".divice_header_frist").fadeToggle();
	} else if($(".divice_header_frist").css("display") == 'none') {
		$(".divice_header_frist").slideToggle();
		$(".add").addClass("device-add");
		$(".file").addClass("device-file");
		$(".file").removeClass("device_file");
		$(".add").removeClass("device_add");
	}
});
$("#getDevice_second").click(function() {
	if($(".divice_header_second").css("display") == 'block') {
		$(".Add").addClass("device_add");
		$(".File").addClass("device_file");
		$(".File").removeClass("device-file");
		$(".Add").removeClass("device-add");
		$(".divice_header_second").fadeToggle();
	} else if($(".divice_header_second").css("display") == 'none') {
		$(".divice_header_second").slideToggle();
		$(".Add").addClass("device-add");
		$(".File").addClass("device-file");
		$(".File").removeClass("device_file");
		$(".Add").removeClass("device_add");
	}
});
//			全体打钩
function checkInput() {
	var tick = $(".tick").css("display");
	if(tick == "none") {
		$(".tick").show();
	} else if(tick == "block") {
		$(".tick").hide();
		$(".tick").hide();
		$(".tick_block ").hide();
	}
}
//			单独打钩
var tickId = 0;
var tickaId = '';
var somearray = []

function removeByValue(arr, val) {
	for(var i = 0; i < arr.length; i++) {
		if(arr[i] == val) {
			arr.splice(i, 1);
			break;
		}
	}
}

function checkInputA(data) {
	var ticka = "tick_" + data.id
	var tickBlock = document.getElementById(ticka);
	if(tickBlock.style.display == "block") {
		tickBlock.style.display = "none";
		removeByValue(somearray, data.id);
		tickId = tickId - 1;
		tickaId = somearray[0];
	} else if(tickBlock.style.display == "" || tickBlock.style.display == "none") {
		tickBlock.style.display = "block";
		removeByValue(somearray, data.id);
		somearray.push(data.id);
		tickaId = somearray[0];
		tickId = tickId + 1;
	}
}
//双击
function ondblInput() {
	$(".device_hardware").fadeToggle();
}

//		设备编辑
$(".device_edit").click(function() {
	if(tickId == 0) {
		alert("请选择你要编辑的记录！")
	} else if(tickId == 1) {
		window.location.href = 'deviceRevise.jsp';
		sessionStorage.setItem("tickId", tickId);
		sessionStorage.setItem("tickaId", tickaId);

	} else if(tickId > 1) {
		alert("一次只能编辑一条记录！")
	}
})

$(".device_logout").click(function() {
	if(tickId == 0) {
		alert("请选择你要编辑的记录！")
	} else if(tickId == 1) {

		$.ajax({
			url: 'updateDeviceInfo',
			type: 'POST',
			data: {
				session: $.Cookie("session"),
				userId: $.Cookie("userId"),
				flag: "1",
				id: tickaId,
				status: "2"
			},
			dataType: 'json',
			success: function(data) {
				if(data.errorCode == 0) {
					window.location.href = 'device.jsp';

				} else if(data.errorCode == -6) {
					alert("登录超时！")
					top.location.href = 'index.jsp';
				}
			},
			error: function(err) {
				alert("连接服务器失败！请重新提交！")
				top.location.href = 'index.jsp';

			}
		});

	} else if(tickId > 1) {
		alert("一次只能编辑一条记录！")
	}
})
$(".device_start").click(function() {
	if(tickId == 0) {
		alert("请选择你要编辑的记录！")
	} else if(tickId == 1) {
		$.ajax({
			url: 'updateDeviceInfo',
			type: 'POST',
			data: {
				session: $.Cookie("session"),
				userId: $.Cookie("userId"),
				flag: "1",
				id: tickaId,
				status: "0"
			},
			dataType: 'json',
			success: function(data) {
				if(data.errorCode == 0) {
					window.location.href = 'device.jsp';
				} else if(data.errorCode == -6) {
					alert("登录超时！")
					top.location.href = 'index.jsp';
				}

			},
			error: function(err) {
				alert("连接服务器失败！请重新提交！")
				top.location.href = 'index.jsp';
			}
		});

	} else if(tickId > 1) {
		alert("一次只能编辑一条记录！")
	}
})
$(".device_ban").click(function() {
	if(tickId == 0) {
		alert("请选择你要编辑的记录！")
	} else if(tickId == 1) {
		$.ajax({
			url: 'updateDeviceInfo',
			type: 'POST',
			data: {
				session: $.Cookie("session"),
				userId: $.Cookie("userId"),
				flag: "1",
				id: tickaId,
				status: "1"
			},
			dataType: 'json',
			success: function(data) {
				if(data.errorCode == 0) {
					window.location.href = 'device.jsp';
				} else if(data.errorCode == -6) {
					alert("登录超时！")
					top.location.href = 'index.jsp';
				}
			},
			error: function(err) {
				alert("连接服务器失败！请重新提交！")
				top.location.href = 'index.jsp';
			}
		});

	} else if(tickId > 1) {
		alert("一次只能编辑一条记录！")
	}
})

autodivheight();

function autodivheight() { //函数：获取尺寸
	//获取浏览器窗口高度
	var winHeight = 0;
	if(window.innerHeight)
		winHeight = window.innerHeight;
	else if((document.body) && (document.body.clientHeight))
		winHeight = document.body.clientHeight;
	//通过深入Document内部对body进行检测，获取浏览器窗口高度
	if(document.documentElement && document.documentElement.clientHeight)
		winHeight = document.documentElement.clientHeight;
	//DIV高度为浏览器窗口的高度
	a = winHeight - 190;
	if(a < 0) {
		a = 0
		$(".pageToolDevice").css("height", a);
		//		$(".pageTool").css("min-height", 410);

	} else {
		$(".pageToolDevice").css("height", a);
		//		$(".pageTool").css("min-height", 410);

	}
	//				document.getElementById("test").style.height = winHeight / 2 + "px";
}
window.onresize = autodivheight; //浏览器窗口发生变化时同时变化DIV高度